require 'rexml/document'
include REXML

class MyText
  public
  attr_reader :sentences, :words

  def initialize file_name
    @words = Set.new
    @sentences = []

    case File.extname file_name
    when ".xml" then parseXML file_name
    when "" then parsePlain file_name
    end
  end

  def add_sentence ns
    new_sentence = ns.downcase.gsub /\.|,|\-|"|\d|;|:|\/|\(|\)|\+|\%/, ""
    new_words = new_sentence.split(" ")
    return unless new_words && new_words.size>0
    @sentences.push new_words
    @words += new_words
  end

  def parsePlain file_name  
    doc = File.open(File.new(file_name))

    doc.each { |el|
      el.split(".").each { |w|
        add_sentence w if w and w.length>0
      }
    }
    @sentences.delete nil
  end

  def parseXML file_name  
    doc = Document.new(File.new(file_name))

    doc.root.elements.each("text/body/div/p") { |el|
      add_sentence el.text
    }
    @sentences.delete nil
  end

end
